package com.awesome.spring.mqtt.listener;

/**
 * @program: awesome-spring
 * @description: Mqtt服务监听
 * @author: Alex Wu
 * @createDate: 2025-03-19 10:45
 **/
import jakarta.annotation.PostConstruct;
import org.eclipse.paho.mqttv5.client.*;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;


@Component
public class MqttListener {

    private static final String MQTT_BROKER = "tcp://192.168.12.101:1883";
    private static final String CLIENT_ID = "springboot-mqtt-subscriber";
    private static final String TOPIC = "test/topic";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "public";

    private final Integer QOS = 1;

    @Autowired
    private  MqttClient client;

    @EventListener(ApplicationReadyEvent.class) // Spring 完全启动后调用
    public void onApplicationReady() {
        try {
            /*MqttClient client = new MqttClient(MQTT_BROKER, CLIENT_ID);
            MqttConnectionOptions options = new MqttConnectionOptions();
//            options.setUserName(USERNAME);
//            options.setPassword(PASSWORD.toCharArray());
            options.setAutomaticReconnect(true);
            client.connect(options);*/


            client.setCallback(new MqttCallback() {


                @Override
                public void disconnected(MqttDisconnectResponse mqttDisconnectResponse) {

                }

                @Override
                public void mqttErrorOccurred(MqttException e) {

                }

                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println("topic: " + topic);
                    System.out.println("qos: " + message.getQos());
                    System.out.println("message content: " + new String(message.getPayload()));

                }

                @Override
                public void deliveryComplete(IMqttToken iMqttToken) {

                }

                @Override
                public void connectComplete(boolean b, String s) {
                }

                @Override
                public void authPacketArrived(int i, MqttProperties mqttProperties) {

                }

                public void connectionLost(Throwable cause) {
                    System.out.println("connectionLost: " + cause.getMessage());
                }

                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("deliveryComplete: " + token.isComplete());
                }
            });

            int qos = 1;
            client.subscribe(TOPIC, qos);




          /*  client.subscribe(TOPIC, QOS, (topic, message) -> {
                System.out.println("收到 MQTT 消息: " + new String(message.getPayload()));
            });

            System.out.println("已订阅 MQTT 主题: " + TOPIC);*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

